<?php
class FavorsController extends AppController {

	public $name = 'Favors';
	
	public $components = array('Bayon');
	
	public $result = array( 'result' => '',
                            'message' => '',
                            'errors' => '',
                            'data' => ''
                            );
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->setLayout();
		$this->Auth->allow('*');
	}
	public function beforeRender() {
        
        $this->set( 'result', $this->result );   
        }

        public function viewAll($userId){
            $favors = $this->Favor->find('all', array('conditions' => array('Favor.user_id' => $userId)));
            pr($favors); die();
        }
        public function add(){
		if( !empty( $this->data ) ){
			
			$this->data['Favor']['user_id'] = $this->Bayon->userId();
			
			$this->Favor->create();
			if($this->Favor->save($this->data)){
				$this->result['result'] = 1;
				$this->result['data'] = $this->data;
				$this->result['message'] = __('Favor is added', true);
				$this->set('noticeId', $this->data['Favor']['notice_id']);
			}
		}
	}
	
	public function disfavor(){
		
		if( !empty( $this->data ) ){
			
			if($this->Favor->deleteAll(
				array('Favor.user_id' => $this->Bayon->userId(),
					  'Favor.notice_id' => $this->data['Favor']['notice_id']
				),
				false
			)){
			
				$this->result['result'] = 1;
				$this->result['message'] = __('Favor is removed', true);
				$this->set('noticeId', $this->data['Favor']['notice_id']);
			
			}else{
				$this->result['result'] = 0;
				$this->result['message'] = __('Favor is not removed', true);	
			}
			
		}
	}

}
?>